<?php

namespace common\behavior\CommonMessagePushing;


use common\service\BehaviorService;
use Webman\Stomp\Client;

/**
 * 延期待审核 批量 消息通知
 * todo 合并 单条/批量 消息通知逻辑
 */
class DelayReviewBatchBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'task_entity' => '',
                'review_man_data' => '',
                'review_request' => '',
                'creator' => '',
                'message_template_id' => 'intval',
            ],
            [
                'task_entity' => [],
                'review_man_data' => [],
                'review_request' => [],
                'creator' => [],
                'message_template_id' => 0,
            ],
            'data',
            ['type' => 'task_delay_review_batch'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($taskEntity, $reviewManData, $currentReviewRequest, $creator, $messageTemplateId) = $params;
        if (empty($taskEntity) || empty($messageTemplateId) || empty($reviewManData) || empty($currentReviewRequest) || empty($creator)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        $currentReviewRequest['param'] = json_decode($currentReviewRequest['param'], true);

        if (!isset($reviewManData[$currentReviewRequest['review_request_node']['plan_reviewer']])) {
            return;
        }

        $receiver = $reviewManData[$currentReviewRequest['review_request_node']['plan_reviewer']];

        $messageParam = [
            'first' => '有新的延期申请',
            'task_name' => get_chat_entity_task_name($taskEntity['entity'], $taskEntity['task']),
            'sender_name' => $creator['name'],
            'task_delay_time' => date("Y/m/d H:i", $currentReviewRequest['param']['task_delay_time']),
            'remark' => '请及时审核',
            'task_id' => $currentReviewRequest['link_id'],
            'review_request_id' => ['id'],
            'review_request_node_id' => $currentReviewRequest['review_request_node']['id'],
        ];

        /***********  发送新的队列  ************/
        Client::send('common_message_pushing', build_queue_data_with_xu([
            'message_generated' => [[
                "param" => $messageParam,
                'receiver' => $receiver,
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_review',
                'is_popup' => 1,
                'message_key' => 'task_delay_need_review',
            ]],
        ]));
        /***********  结束  ************/
    }
}
